package com.zxy.personage.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.common.utils.PageUtils;
import com.zxy.common.utils.Query;

import com.zxy.personage.dao.AttentionDao;
import com.zxy.personage.entity.AttentionEntity;
import com.zxy.personage.service.AttentionService;


@Service("attentionService")
public class AttentionServiceImpl extends ServiceImpl<AttentionDao, AttentionEntity> implements AttentionService {
    @SuppressWarnings("all")
    @Autowired
    RestHighLevelClient client;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttentionEntity> page = this.page(
                new Query<AttentionEntity>().getPage(params),
                new QueryWrapper<AttentionEntity>()
        );

        return new PageUtils(page);
    }
    


    
    @Override
    public PageUtils eslist(Map<String, Object> params) {
        try {
            SearchRequest searchRequest = new SearchRequest("sson");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());

            String page = (String) params.get("page");
            String limit = (String) params.get("limit");

            searchRequest.source(searchSourceBuilder);
            searchSourceBuilder.from((Integer.parseInt(page)-1)*Integer.parseInt(limit));
            searchSourceBuilder.size(Integer.parseInt(limit));
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = searchResponse.getHits();
            TotalHits totalHits = hits.getTotalHits();
// the total number of hits, must be interpreted in the context of totalHits.relation
            int numHits = (int) totalHits.value;
// whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
            TotalHits.Relation relation = totalHits.relation;
            float maxScore = hits.getMaxScore();
            SearchHit[] searchHits = hits.getHits();
            ArrayList<AttentionEntity> list = new ArrayList<>();
            for (SearchHit hit : searchHits) {
                // do something with the SearchHit
                String index = hit.getIndex();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                AttentionEntity attentionEntity = JSON.parseObject(sourceAsString, new TypeReference<AttentionEntity>() {
                });
                attentionEntity.setGid(Integer.valueOf(id));
                list.add(attentionEntity);
            }
            return new PageUtils(list,numHits,Integer.valueOf(limit),Integer.valueOf(page));
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

}